package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Movies;
import com.itheima.domain.MoviesExample;

import com.itheima.mapper.MoviesMapper;
import com.itheima.service.MoviesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class MoviesServiceImpl implements MoviesService {
    @Autowired
    private MoviesMapper moviesMapper;


    @Override
    public PageInfo<Movies> findByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        MoviesExample moviesExample = new MoviesExample();
        moviesExample.setOrderByClause("show_time desc");

        List<Movies> list = moviesMapper.selectByExample(moviesExample);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Movies> findByPage(Integer pageNum, Integer pageSize, Integer cid, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        MoviesExample moviesExample = new MoviesExample();
        moviesExample.setOrderByClause("show_time desc");
        MoviesExample.Criteria criteria=moviesExample.createCriteria();
        if (cid!=null){
            criteria.andCidEqualTo(cid);
        }
        if (keyword!=null && !"".equals(keyword)){
            criteria.andKeywordLike("%"+keyword+"%");
    }

        List<Movies> list = moviesMapper.selectByExample(moviesExample);
        return new PageInfo<>(list);
    }

    @Override
    public void save(Movies movies) {
        Integer id=movies.getId();
        if (id==null){

        moviesMapper.insertSelective(movies);
        }else {
            moviesMapper.updateByPrimaryKeySelective(movies);
        }


    }

    @Override
    public Movies findById(Integer id) {
        return moviesMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteById(Integer id) {
        moviesMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteBatch(Integer[] ids) {
        for (Integer id : ids) {

        moviesMapper.deleteByPrimaryKey(id);
        }
    }
}
